package lwh.utils;

/**
 * Portal常用工具
 *
 */
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PortalUtil {

	 public static String getFilePath(String fileName) {           
//		 ProtectionDomain domain = PortalUtil.class.getProtectionDomain();           
//		 CodeSource codeSource = domain.getCodeSource();          
//		 URL location = codeSource.getLocation();           
//		 String path = location.getPath();          
//		 System.out.println(path);                     
		 URL url = PortalUtil.class.getClassLoader().getResource(fileName);          
		 String aurl = url.getPath();          
//		 System.out.println(aurl);              
//		 URL pathUrl = PortalUtil.class.getResource("");          
//		 System.out.println(pathUrl.getPath());   
		 return aurl;
		 }   
	/**
		 * 获取项目的相对路径下文件的绝对路径
		 * 
		 * @param parentDir
		 *            目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or
		 *            bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径
		 * @param fileName
		 *            文件名
		 * @return 一个绝对路径
		 */
		public static String getPath(String parentDir, String fileName) {
			String path = null;
			String userdir = System.getProperty("user.dir");
			String userdirName = new File(userdir).getName();
			if (userdirName.equalsIgnoreCase("lib")
					|| userdirName.equalsIgnoreCase("bin")) {
				File newf = new File(userdir);
				File newp = new File(newf.getParent());
				if (fileName.trim().equals("")) {
					path = newp.getPath() + File.separator + parentDir;
				} else {
					path = newp.getPath() + File.separator + parentDir
							+ File.separator + fileName;
				}
			} else {
				if (fileName.trim().equals("")) {
					path = userdir + File.separator + parentDir;
				} else {
					path = userdir + File.separator + parentDir + File.separator
							+ fileName;
				}
			}

			return path;
		}
	/**
	 * 取得system.properties
	 */
    public static Map getSystemPropertiesValue(String [] keyArr) {

		String path = getFilePath("system.properties");
		Properties p = new Properties();
		
		try {
			p.load(new FileInputStream(path));
		} catch (Exception e) {
		}
		
		Map domainMap = new HashMap();
		
		for (int i = 0; i < keyArr.length; i++){
			String key = keyArr[i];
			String value = "";
			value = (String)p.getProperty(key);
			domainMap.put(key, value);
    	}
		return domainMap;
	}
    
}